﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMeterParamFrame.Model.DIValueConverters
{

    /// <summary>
    /// 电价 转换类，负责转换所有的电价
    /// 包括各个费率电价，阶梯电价
    /// 其转换格式符合xxxx.xxxx或者 xxxx
    /// 所有的 静态变量，采用拼音编写
    /// </summary>
    class Converter_FeiLvDiaJia : IDIDataConverter
    {

        /// <summary>
        /// 第一套费率的 起始di 值
        /// </summary>
        private static uint MIN_FEILV1 = uint.Parse("04050101", System.Globalization.NumberStyles.AllowHexSpecifier);
        private static uint MAX_FEILV1 = uint.Parse("0405013F", System.Globalization.NumberStyles.AllowHexSpecifier);

        /// <summary>
        /// 第二套费率的 起始di 值
        /// </summary>
        private static uint MIN_FEILV2 = uint.Parse("04050201", System.Globalization.NumberStyles.AllowHexSpecifier);
        private static uint MAX_FEILV2 = uint.Parse("0405023F", System.Globalization.NumberStyles.AllowHexSpecifier);

        /// <summary>
        /// 第一套阶梯电价起始值
        /// </summary>
        private static uint MIN_JEITIDIANJIA1 = uint.Parse("04060101", System.Globalization.NumberStyles.AllowHexSpecifier);
        private static uint MAX_JEITIDIANJIA1 = uint.Parse("04060163", System.Globalization.NumberStyles.AllowHexSpecifier);

        /// <summary>
        /// 第二套阶梯电价起始值
        /// </summary>
        private static uint MIN_JEITIDIANJIA2 = uint.Parse("04060301", System.Globalization.NumberStyles.AllowHexSpecifier);
        private static uint MAX_JEITIDIANJIA2 = uint.Parse("04060363", System.Globalization.NumberStyles.AllowHexSpecifier);
        public bool CanConverter(string di)
        {
            if (string.IsNullOrEmpty(di)) return false;
            uint udi = uint.Parse(di, System.Globalization.NumberStyles.AllowHexSpecifier);

            if (udi >= MIN_FEILV1 && udi <= MAX_FEILV1)
                return true;

            if (udi >= MIN_FEILV2 && udi <= MAX_FEILV2)
                return true;

            if (udi >= MIN_JEITIDIANJIA1 && udi <= MAX_JEITIDIANJIA1)
                return true;

            if (udi >= MIN_JEITIDIANJIA2 && udi <= MAX_JEITIDIANJIA2)
                return true;

            return false;
        }


        public string ConvertToUI(string di, string value)
        {
            if (value.Length != 8)
            {
                throw new ArgumentException("DI(" + di + "),Value(" + value + ")");
            }

            string[] vals = new string[2];
            string newValue = "";

            vals[0] = value.Substring(0, 4);
            vals[1] = value.Substring(4, 4);

            //处理整数部分
            int lastPositiveIndex = vals[0].IndexOfAny(DIValueConverter.NUMBER_POSITIVE);
            if (lastPositiveIndex >= 0)
            {
                newValue = vals[0].Substring(lastPositiveIndex, vals[0].Length - lastPositiveIndex);
            }
            else
            {
                newValue = "0";
            }

            //小数点
            newValue += ".";

            //小数部分
            lastPositiveIndex = vals[1].LastIndexOfAny(DIValueConverter.NUMBER_POSITIVE);
            if (lastPositiveIndex >= 0)
            {
                newValue += vals[1].Substring(0, lastPositiveIndex + 1);
            }
            else
            {
                newValue += "0";
            }

            return newValue;
        }


        public string ConvertToLow(string di, string value)
        {
            string newValue = "";
            string[] ss = value.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            ss[0] = ss[0].PadLeft(4, '0');
            newValue = ss[0];
            if (ss.Length >= 2)
            {
                ss[1] = ss[1].PadRight(4, '0');
                newValue += ss[1];
            }
            else
            {
                newValue += "0000";
            }
            return newValue;
        }


        public bool ValidateValue(string di, string value)
        {
            return true;
        }
    }
}
